package com.guobox.movies.nasManager;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;

class NasIpObserve extends ContentObserver {

    static final String NAS_PARAMETERS = "NasParameters";
    static final String NAS_ID = "nasId";
    static final String NAS_IP = "nasIp";
    static final String NAS_NAME = "nasName";

    public static final String AUTHORITY = "com.guobox.player.nas.provider";
    private static final Uri NOTIFY_URI = Uri.parse("content://" + AUTHORITY + "/info");
    private Handler handler;
    private NasIpCallback nasIpCallback;
    private Context context;
    private ContentResolver contentResolver;
    public NasIpObserve(Handler handler, Context context) {
        super(handler);
        this.context = context;
        contentResolver = context.getContentResolver();
    }

    public void addCallback(NasIpCallback nasIpCallback) {
        this.nasIpCallback = nasIpCallback;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        String ip = null;
        Cursor cursor = contentResolver.query(NOTIFY_URI, new String[]{NAS_IP}, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            ip = cursor.getString(cursor.getColumnIndex(NAS_IP));
            cursor.close();
        }
        if (nasIpCallback != null) {
            nasIpCallback.onNasIpCallback(ip);
        }
    }
}